unit doublebackgroundselection;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  ButtonPanel, BGRABitmap, BGRABitmapTypes;

type

  { TDoubleBackgroundSelectionForm }

  TDoubleBackgroundType = (dbtBattle, dbtTitle);

  TDoubleBackgroundSelectionForm = class(TForm)
    Bg1ListBox: TListBox;
    Bg1Bg2Splitter: TSplitter;
    Bg2ListBox: TListBox;
    Bg2PreviewSplitter: TSplitter;
    ResultButtonPanel: TButtonPanel;
    PreviewPaintBox: TPaintBox;
    PreviewScrollBox: TScrollBox;
    procedure BgListBoxClick(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure PreviewPaintBoxPaint(Sender: TObject);
  private
    DoubleBgType: TDoubleBackgroundType;
    ResultWasAccepted: Boolean;
    Previews: array [1..2] of TBGRACustomBitmap;
    procedure FillListBox(Box: TListBox; Subfolder: String; CurrentValue: String);
  public
    SelectedBgs: array [1..2] of String;
    function ShowBattleBgSelection(Bg1, Bg2: String): Boolean;
    function ShowTitleBgSelection(Bg1, Bg2: String): Boolean;
  end;

var
  DoubleBackgroundSelectionForm: TDoubleBackgroundSelectionForm;

implementation

uses
  filehelper, globals, constants;

{$R *.lfm}

{ TDoubleBackgroundSelectionForm }

procedure TDoubleBackgroundSelectionForm.BgListBoxClick(Sender: TObject);
var
  BgType: Integer;
  Box: TListBox;
  NewBg: String;
begin
  Box := (Sender as TListBox);
  BgType := Box.Tag;

  if Box.ItemIndex = 0 then
    NewBg := ''
  else
    NewBg := Box.Items[Box.ItemIndex];

  if NewBg <> SelectedBgs[BgType] then begin
    SelectedBgs[BgType] := NewBg;

    Previews[BgType].Free;
    Previews[BgType] := nil;
    if DoubleBgType = dbtBattle then
      Previews[BgType] := Game.GetScaledBattlebackImgBGRA(NewBg, BgType,
                                       PreviewPaintBox.Width / SCREEN_WIDTH)
    else if DoubleBgType = dbtTitle then
      Previews[BgType] := Game.GetScaledTitleImgBGRA(NewBg, BgType,
                                     PreviewPaintBox.Width / SCREEN_WIDTH);
    PreviewPaintBox.Refresh;
  end;
end;

procedure TDoubleBackgroundSelectionForm.CancelButtonClick(Sender: TObject);
begin
  ResultWasAccepted := False;
  Close
end;

procedure TDoubleBackgroundSelectionForm.OKButtonClick(Sender: TObject);
begin
  ResultWasAccepted := True;
  Close
end;

procedure TDoubleBackgroundSelectionForm.PreviewPaintBoxPaint(Sender: TObject);
begin
  if Previews[1] <> nil then
    Previews[1].Draw(PreviewPaintBox.Canvas, 0, 0, True);
  if Previews[2] <> nil then
    Previews[2].Draw(PreviewPaintBox.Canvas, 0, 0, False);
end;

procedure TDoubleBackgroundSelectionForm.FillListBox(Box: TListBox;
  Subfolder: String; CurrentValue: String);
var
  Path: String;
  FileName: String;
  Files: TFilenameList;
  PreselectedIndex: Integer = -1;
begin
  Box.Items.BeginUpdate;
  try
    Box.Items.Clear;
    Box.Items.Add(NoneFilename);
    if (CurrentValue = '') then begin
      PreselectedIndex := Box.Items.Count - 1;
    end;

    Path := Game.GetImageSubfolderPath(Subfolder);
    Files := FillFilenameList(Path);
    try
      for FileName in Files do begin
        Box.Items.Add(FileName);
        if CurrentValue = FileName then begin
          PreselectedIndex := Box.Items.Count - 1;
        end;
      end;
    finally
      Files.Free;
    end;
  finally
    Box.Items.EndUpdate;
  end;

  Box.ItemIndex := PreselectedIndex;
end;

function TDoubleBackgroundSelectionForm.ShowBattleBgSelection(Bg1, Bg2: String
  ): Boolean;
begin
  ResultWasAccepted := False;

  DoubleBgType := dbtBattle;

  FillListBox(Bg1ListBox, 'battlebacks1', Bg1);
  FillListBox(Bg2ListBox, 'battlebacks2', Bg2);

  if Bg1 <> '' then
    Previews[1] := Game.GetScaledBattlebackImgBGRA(Bg1, 1,
                                     PreviewPaintBox.Width / SCREEN_WIDTH);
  if Bg2 <> '' then
    Previews[2] := Game.GetScaledBattlebackImgBGRA(Bg2, 2,
                                     PreviewPaintBox.Width / SCREEN_WIDTH);

  ShowModal;

  ShowBattleBgSelection := ResultWasAccepted
end;

function TDoubleBackgroundSelectionForm.ShowTitleBgSelection(Bg1, Bg2: String
  ): Boolean;
begin
  ResultWasAccepted := False;

  DoubleBgType := dbtTitle;

  FillListBox(Bg1ListBox, 'titles1', Bg1);
  FillListBox(Bg2ListBox, 'titles2', Bg2);

  if Bg1 <> '' then
    Previews[1] := Game.GetScaledTitleImgBGRA(Bg1, 1,
                                     PreviewPaintBox.Width / SCREEN_WIDTH);
  if Bg2 <> '' then
    Previews[2] := Game.GetScaledTitleImgBGRA(Bg2, 2,
                                     PreviewPaintBox.Width / SCREEN_WIDTH);

  ShowModal;

  ShowTitleBgSelection := ResultWasAccepted
end;

end.

